package com.example.websocket.controller;

import com.example.websocket.model.Message;
import com.example.websocket.service.ChatSocketServer;
import com.example.websocket.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Set;

/**
 * @author dangwen1
 * @date 2022年01月14日 15:23
 * @description
 */
@Controller
public class ChatController {

    @Autowired
    private ChatSocketServer chatSocketServer;

    @GetMapping("/")
    public String index(){
        return "chat";
    }

    @GetMapping("/chat/send")
    @ResponseBody
    public void send(String from, String to, String message) {
        chatSocketServer.sendMessage(new Message(from,to,message,new Date()));
    }

    //在线用户列表
    @GetMapping("/getOnlineUser")
    @ResponseBody
    public Set<String> getOnlineUser() {
        return chatSocketServer.clients.keySet();
    }
}
